package com.csnf.servlet;

import com.csnf.entity.Singger;
import com.csnf.service.SinggerService2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/singger")
public class SinggerServlet2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    SinggerService2 service2=new SinggerService2();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        //获取type值
        String type=req.getParameter("type");
        //根据type值判断执行哪个操作
        //type.equals("all")
        if("all".equals(type)){
            //歌手信息展示
            String singerName=req.getParameter("singerName");
            String sex=req.getParameter("sex");
            String piaoshu1=req.getParameter("piaoshu1");
            String piaoshu2 =req.getParameter("piaoshu2");
            String page=req.getParameter("page");
            if (null==page ||"".equals(page)){
                page="1";
            }
            String y="3";
            String x= ((Integer.valueOf(page)-1)*Integer.valueOf(y))+"";
            List<Singger> slist= service2.getSingger(singerName,sex,piaoshu1,piaoshu2,x,y);
            System.out.println(slist);
            //获取歌手总条数
            int allcount=service2.getSingerCount();
            //计算一共有多少页
            int allPage=0;
            if(allcount % Integer.valueOf(y)==0){
                allPage=allcount / Integer.valueOf(y);
            }else{
                allPage=allcount / Integer.valueOf(y)+1;
            }
            //将一共多少页保存
            req.setAttribute("allPage",allPage);
            //将歌手信息保存到slist中
            req.setAttribute("slist", slist);
            //保存当前几页
            req.setAttribute("page",page);
            //进入index页面
            req.getRequestDispatcher("index.jsp").forward(req, resp);
        } else if ("toupiao".equals(type)) {
            //歌手投票更新
            //获取singgerId
            //window.location.href="singger?type=toupiao&sid="+singerid;
            String sid=  req.getParameter("sid");
            System.out.println("sid="+sid);
            int i=service2.updateToupiao(sid);
            if(i>0){
                req.setAttribute("msg","1");
            }else {
                req.setAttribute("msg","0");
            }
            //不能直接去index.jsp，因为页面上的歌手信息要通过singger/type=all查询
            req.getRequestDispatcher("singger?type=all").forward(req, resp);
        }


        //新增歌手
        //删除歌手
        //修改歌手
    }
}
